package com.jpro.framework.common.util.string;

import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.hutool.core.text.StrPool.COMMA;

/**
 * 字符串工具类
 *
 * @author JPROCMS
 */
@Component
public class StrUtils {


    static final String LATEX_SPAN_REG = "<span data-w-e-type=(.*?)</span>";
    static final String LATEX_DATA_VAL_REG = "data-value=['\"]?(.*?)['\"]";
    static final String LATEX_SPT = "$";

    private static boolean latexOpen = false;

    public StrUtils(@Value("${jpro.cms.latexOpen}") Boolean latexOpen) {
        this.latexOpen = latexOpen;
    }

    public static String replaceLatex(String text) {
        if (!latexOpen) {
            return text;
        }
        //富文本编辑器的添加的公式特征 <span data-w-e-type="formula" data-w-e-is-void="" data-w-e-is-inline="" data-value="c = \\pm\\sqrt{a^2 + b^2}"></span>
        //带公式的span
        final List<String> spanDocuments = ReUtil.findAllGroup0(LATEX_SPAN_REG, text);
        for (String s : spanDocuments) {
            //具体公式
            final List<String> latexs = ReUtil.findAllGroup1(LATEX_DATA_VAL_REG, s);
            if (latexs.size() > 0) {
                String latex = latexs.get(0);
                //span整个元素替换为公式 $c = \\pm\\sqrt{a^2 + b^2}$   $是前端插件显示需要
                text = text.replace(s, LATEX_SPT + latex + LATEX_SPT);
            }
        }
        return text;
    }

    public static String maxLength(CharSequence str, int maxLength) {
        return StrUtil.maxLength(str, maxLength - 3); // -3 的原因，是该方法会补充 ... 恰好
    }

    /**
     * 给定字符串是否以任何一个字符串开始
     * 给定字符串和数组为空都返回 false
     *
     * @param str      给定字符串
     * @param prefixes 需要检测的开始字符串
     * @since 3.0.6
     */
    public static boolean startWithAny(String str, Collection<String> prefixes) {
        if (StrUtil.isEmpty(str) || ArrayUtil.isEmpty(prefixes)) {
            return false;
        }

        for (CharSequence suffix : prefixes) {
            if (StrUtil.startWith(str, suffix, false)) {
                return true;
            }
        }
        return false;
    }

    public static List<Long> splitToLong(String value, CharSequence separator) {
        long[] longs = StrUtil.splitToLong(value, separator);
        return Arrays.stream(longs).boxed().collect(Collectors.toList());
    }

    public static Set<Long> splitToLongSet(String value) {
        return splitToLongSet(value, StrPool.COMMA);
    }

    public static Set<Long> splitToLongSet(String value, CharSequence separator) {
        long[] longs = StrUtil.splitToLong(value, separator);
        return Arrays.stream(longs).boxed().collect(Collectors.toSet());
    }

    public static List<Integer> splitToInteger(String value, CharSequence separator) {
        int[] integers = StrUtil.splitToInt(value, separator);
        return Arrays.stream(integers).boxed().collect(Collectors.toList());
    }

    public static List<String> splitToString(String value, CharSequence separator) {
        return StrUtil.split(value, separator);
    }

    public static List<Long> splitToLong(String value) {
        if (value == null) {
            return null;
        }
        return StrUtils.splitToLong(value, COMMA);
    }
    public static List<String> splitToString(String value) {
        if (!StringUtils.hasText(value)) {
            return null;
        }
        return StrUtil.split(value, COMMA);
    }


    /**
     * 移除字符串中，包含指定字符串的行
     *
     * @param content 字符串
     * @param sequence 包含的字符串
     * @return 移除后的字符串
     */
    public static String removeLineContains(String content, String sequence) {
        if (StrUtil.isEmpty(content) || StrUtil.isEmpty(sequence)) {
            return content;
        }
        return Arrays.stream(content.split("\n"))
                .filter(line -> !line.contains(sequence))
                .collect(Collectors.joining("\n"));
    }

}
